package project.dade.cn.bst.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.AppCompatTextView;
import android.text.Html;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;

import butterknife.InjectView;
import project.dade.cn.bst.R;
import project.dade.cn.bst.base.ZJPBaseBarActivity;
import project.dade.cn.bst.manager.AppConfig;
import project.dade.cn.bst.utils.AESUse;
import project.dade.cn.bst.utils.LogUtil;
import project.dade.cn.bst.utils.ToastUtil;
import project.dade.cn.bst.utils.ValidateUtil;

/**
 * 注册页面
 */
public class RegActivity extends ZJPBaseBarActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    private static String TAG = RegActivity.class.getSimpleName();
    @InjectView(R.id.accountEt)
    EditText accountEt;//注册账号
    @InjectView(R.id.passEt)
    EditText passEt;//注册密码
    @InjectView(R.id.passCEt)
    EditText passCEt;//注册确认密码
    @InjectView(R.id.agreementCb)
    AppCompatCheckBox agreementCb; //协议选中状态
    @InjectView(R.id.agreementTv)
    AppCompatTextView userLicensing; //用户协议
    @InjectView(R.id.regBtn)
    AppCompatButton regBtn; //注册
    private ExitBroadcastReceiver receiver = new ExitBroadcastReceiver();
   public String code="";

    public class ExitBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null && AppConfig.ACTION_REG_SUCESS_FINISH.equals(intent.getAction())) {
                LogUtil.i(TAG, "退出注册页面");
                finish();
            }
        }

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        initData();
        initSetListener();
        //注册退出广播
        IntentFilter filter = new IntentFilter();
        filter.addAction(AppConfig.ACTION_REG_SUCESS_FINISH);
        registerReceiver(receiver, filter);
        regBtn.setOnClickListener(this);
        userLicensing.setOnClickListener(this);
        agreementCb.setOnCheckedChangeListener(this);
    }

    private void initData() {
        userLicensing.setText(Html.fromHtml("《<u>" + "用户协议" + "</u>》"));
    }

    private void initSetListener() {


    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.regBtn:
                //数据验证
                if (isCheckInput()) {
                    try {
                        code= AESUse.getInstance().encrypt(passEt.getText().toString().trim());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    Intent mIntentRegInfo = new Intent(this, RegPhoneVerActivity.class);
                    mIntentRegInfo.putExtra("account", accountEt.getText().toString().trim());
                    mIntentRegInfo.putExtra("pass", code);
                    startActivity(mIntentRegInfo);
                }

                break;
            case R.id.agreementTv:
                //用户协议
                Intent mIntentUserLicensing = new Intent(this, BrowserActivity.class);
               mIntentUserLicensing.putExtra("key", BrowserActivity.USERLICENSING);
                startActivity(mIntentUserLicensing);
                break;

        }

    }

    private boolean isCheckInput() {

        //账号不能为空
        if (TextUtils.isEmpty(accountEt.getText().toString().trim())) {
            ToastUtil.showShort(this, R.string.tips_phone_right);
            YoYo.with(Techniques.Shake)
                    .playOn(findViewById(R.id.accountEt));
            return false;
        }
        //验证手机号码
        if (!ValidateUtil.isMobile(accountEt.getText().toString().trim())) {
            ToastUtil.showShort(this, R.string.tips_phone_right);
            YoYo.with(Techniques.Shake)
                    .playOn(findViewById(R.id.accountEt));
            return false;
        }
        //密码不能为空
        if (TextUtils.isEmpty(passEt.getText().toString().trim())) {
            ToastUtil.showShort(this, R.string.tips_password_null);
            YoYo.with(Techniques.Shake)
                    .playOn(findViewById(R.id.passEt));
            return false;
        }
        //密码长度
        int password_length = passEt.getText().toString().trim().length();
        if (password_length < 6 || password_length > 30) {
            YoYo.with(Techniques.Shake)
                    .playOn(findViewById(R.id.passEt));
            ToastUtil.showShort(this, "密码过于简单，建议修改为英文字母和数字的组合");
            return false;
        }
        //注册确认密码
        if (TextUtils.isEmpty(passCEt.getText().toString().trim())) {
            YoYo.with(Techniques.Shake)
                    .playOn(findViewById(R.id.passCEt));
            ToastUtil.showShort(this, R.string.tips_password_null);
            return false;
        }
        //密码长度
        int cpassword_length = passCEt.getText().toString().trim().length();
        if (cpassword_length < 6 || cpassword_length > 30) {
            YoYo.with(Techniques.Shake)
                    .playOn(findViewById(R.id.passCEt));
            ToastUtil.showShort(this, "密码过于简单，建议修改为英文字母和数字的组合");
            return false;
        }
        if (!passEt.getText().toString().equals(passCEt.getText().toString())) {
            YoYo.with(Techniques.Shake)
                    .playOn(findViewById(R.id.passEt));
            YoYo.with(Techniques.Shake)
                    .playOn(findViewById(R.id.passCEt));
            ToastUtil.showShort(this, "两次密码输入不一致");
            return false;
        }
        if (!agreementCb.isChecked()) {
            ToastUtil.showShort(this, "用户协议未选中");
            return false;
        }

        return true;
    }
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            this.finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}
